package com.rzn.reading.controller;


import com.rzn.reading.JsonResult;
import com.rzn.reading.model.dto.discuss.DiscussionCountDTO;
import com.rzn.reading.model.dto.discuss.DiscussionCreateInputDTO;
import com.rzn.reading.model.dto.discuss.DiscussionItemDetailDTO;
import com.rzn.reading.model.dto.discuss.QueryDiscussDTO;
import com.rzn.reading.service.DiscussionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讨论区表 前端控制器
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@RestController
@RequestMapping("//discussion")
@Api(tags = "讨论区")
public class DiscussionController {
    @Autowired
    private DiscussionService discussionService ;

    @PostMapping("/insert")
    @ApiOperation("添加讨论")
    public JsonResult insertDiscuss(@RequestBody DiscussionCreateInputDTO discussionCreateInputDTO){
        Boolean res = discussionService.insertDiscussion(discussionCreateInputDTO);
        if (res){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }

    @GetMapping("/queryDiscussCount/{literatureId}")
    @ApiOperation("查询心得体会个数")
    public JsonResult queryDiscuss(@PathVariable Integer literatureId){
        DiscussionCountDTO discussionCount = discussionService.getDiscussionCount(literatureId);
        return JsonResult.success(discussionCount);
    }


    @PostMapping("/query")
    @ApiOperation("查询讨论")
    public JsonResult queryDiscuss(@RequestBody QueryDiscussDTO queryDiscussDTO){
        List<DiscussionItemDetailDTO> discussItemList = discussionService.getDiscussItemList(queryDiscussDTO);
        return JsonResult.success(discussItemList);
    }

    @GetMapping("/delete/{id}/{userId}")
    @ApiOperation("删除讨论")
    public JsonResult deleteDiscuss(@PathVariable Integer id,@PathVariable Integer userId){
        Boolean res = discussionService.deleteDiscuss(id, userId);
        if (res){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }
}

